package cn.myzf.fort.access.accesscommon.utils;



import cn.myzf.fort.access.accesscommon.core.ChannelProperties;
import cn.myzf.fort.access.accesscommon.core.impl.YmlProperties;
import org.apache.commons.lang.StringUtils;


/*避免被外部new出来 避免继承 仅在bean容器环境使用*/

public final  class PropertiesStrategyFactory {

    private PropertiesStrategyFactory(){}

    public static ChannelProperties getHostProperties(final String propertiesStrategyClassName) {
        if (StringUtils.isBlank(propertiesStrategyClassName)) {
            return SpringUtil.getBean(YmlProperties.class);
        }
        try {
            Class<?> jobShardingStrategyClass = Class.forName(propertiesStrategyClassName);
            if (!ChannelProperties.class.isAssignableFrom(jobShardingStrategyClass)) {
                throw new IllegalStateException(String.format("Class '%s' is not job strategy class", propertiesStrategyClassName));
            }
            return (ChannelProperties) SpringUtil.getBean(jobShardingStrategyClass);
        } catch (final ClassNotFoundException  ex) {
            throw new IllegalStateException(String.format("HostProperties strategy class '%s' config error, message details are '%s'", propertiesStrategyClassName, ex.getMessage()));
        }
    }
}